var oTable;
    var newCustomer;
    $(document).ready(function(){
        oTable = $('#tblItems').dataTable({
            "bJQueryUI": true,
            "bProcessing": true,
            "iDisplayLength": 10,
            "sPaginationType": "full_numbers",
            "bAutoWidth" : false,
            "sAjaxSource": 'controller?Action=get_all_items',
            "aoColumns": [
                { "sTitle": "ID"      ,"sClass": "center"},
                { "sTitle": "Name"    ,"sClass": "center" },
                { "sTitle": "B/Price" ,"sClass": "center" },
                { "sTitle": "S/Price" ,"sClass": "center" },
                { "sTitle": "Quantity","sClass": "center" },
                { "sTitle": "Type"    ,"sClass": "center" },
                { "sTitle": "Location"    ,"sClass": "center" }
            ]
        });
    });
    $(document).ready(function(){
        $('#customerID').blur(function(){
            var customerID = $('#customerID').val();
            $.post('controller?Action=get_customer&CustomerID='+customerID, function(data){
                if (data=='NewCustomer'){
                    $("#customerName").val('');
                    $("#customerPhone").val('');
                    $("#customerEmail").val('');
                    newCustomer = "true";
                } else {
                    var customer = data.split(",");
                    $("#customerName").val(customer[0]);
                    $("#customerPhone").val(customer[1]);
                    $("#customerEmail").val(customer[2]);
                    newCustomer = "false";
                };
            });
        });

        $("#sellBtn").click(function(){
            var itemID = $('#itemID').val();
            var customerID = $('#customerID').val();
            var customerName = $('#customerName').val();
            var customerEmail = $('#customerEmail').val();
            var customerPhone = $('#customerPhone').val();
            var quantity = $('#quantity').val();

            $.post('controller?Action=sell_items&customerID=' + customerID +
                '&customerName=' +customerName +
                '&customerEmail=' +customerEmail +
                '&customerPhone=' + customerPhone+
                '&itemID=' +itemID +
                '&quantity=' +quantity +
                '&NewCustomer=' + newCustomer,function(data){
                var $dialog = $('<div></div>')
                .html('<center>' + data + '</center>')
                .dialog({
                    autoOpen: false,
                    title: 'Information',
                    buttons: { "OK": function(){
                                $(this).dialog("close");
                            }
                        }
                });
                $dialog.dialog('open');

                $("#customerID").val('');
                $("#customerName").val('');
                $("#customerPhone").val('');
                $("#customerEmail").val('');
                $("#itemID").val('');
                $("#quantity").val('');
            });
        });
    })
